

console.log("get add cart");
setTimeout(() => {
    getTypeIdNeed();
}, 2000);


function getTypeIdNeed(){

   /* $.ajax({
        url: "https://www.ozonzhuli.com/index.php?c=api/neiyi/get_add_cart_type_id",
        success: function (data) {
            if(data.data.type_id_List){
                console.log(data.data.type_id_List);
                getAddCart(data.data.type_id_List, 1,0);
            }
        }
    });*/

    getAddCart(1,0);

}

function getAddCart(page,index){

    console.log("index:"+index +" page:"+page);
    // 创建一个空对象用于保存请求数据
    var dataRequest = {};

    // 设置请求的基本属性
    dataRequest.group_by = "SKU";
    dataRequest.metrics = ["hits_tocart", "hits_tocart_pdp", "hits_tocart_search"];


// 获取当前日期
    var today = new Date();

// 获取七天前的日期
    var sevenDaysAgo = new Date();
    sevenDaysAgo.setDate(today.getDate() - 6); // 设置为七天前

// 格式化日期函数


// 设置 date_from 和 date_to
    dataRequest.date_from = formatDate(sevenDaysAgo) + " 00:00:00"; // 七天前的日期的零点
    dataRequest.date_to = formatDate(today) + " 23:59:59"; // 今天的日期的最后一刻

    console.log(dataRequest);

    // 创建 filters 数组
    dataRequest.filters = [];

    // 设置其他属性
    dataRequest.limit = "10";
    dataRequest.offset = (page-1)*10;
    dataRequest.sort = [
        {
            key: "hits_tocart",
            order: "DESC"
        }
    ];

    //console.log("page_"+type_id_list[index] + "_"+dataRequest.offset);

    // 将 dataRequest 对象转换为 JSON 字符串并输出
    var jsonString = JSON.stringify(dataRequest);
    console.log(jsonString);
     $.ajax({
        url: 'https://seller.ozon.ru/api/site/seller-analytics/charts/table',
　　　　 type: 'POST',
        data:JSON.stringify(dataRequest),
        processData: false,
        contentType: 'application/json',
        headers: {
            'X-O3-App-Name': 'seller-ui',
            'X-O3-Company-Id': '1274960',
            'X-O3-Language': 'zh-Hans',
            'X-O3-Page-Type': 'products-other'
          },
        xhrFields: {
            withCredentials: true // 这里设置了withCredentials
        },
        success: function(data) {
             console.log(data);
             if (data.result.data) {

                 var offer_id_list = [];
                 data.result.data.forEach(item => {
                      console.log(item['dimensions'][0]['sellerId']+"--"+item['metrics'][0]);
                       if(item['metrics'][0] > 0){
                           offer_id_list.push(item['dimensions'][0]['sellerId']+"--"+item['metrics'][0]);
                       }
                 });

                 $.post("https://www.ozonzhuli.com/index.php?c=api/neiyi/post_add_cart",
                      {data:offer_id_list},
                      function(data){
                        //console.log(data);
                      },
                  "text");


                  if(offer_id_list.length==10){
                      setTimeout(() => {
                          page = page + 1;
                          getAddCart(page,index)
                      }, 5000);
                  }else{
                      console.log("sleep one day")
                      setTimeout(() => {
                          location.reload();
                      }, 3600000);
                  }
             }
        },
        error: function(err) {
            console.error(err);
        }
    });
}

function formatDate(date) {
    var year = date.getFullYear();
    var month = String(date.getMonth() + 1).padStart(2, '0'); // 月份从0开始，+1
    var day = String(date.getDate()).padStart(2, '0');

    return `${year}-${month}-${day}`;
}


